<template>
	<view class="container">
		<view class="tui-searchbox">
			<view class="tui-search-input">
				<icon type="search" :size='13' color='#333'></icon>
				<input confirm-type="search" placeholder="输入需要搜索的关键字" :focus="true" auto-focus placeholder-class="tui-input-plholder"
				 class="tui-input" v-model.trim="key" @input="inputKey"/>
				<icon type="clear" :size='13' color='#bcbcbc' @tap="cleanKey" v-show="key"></icon>
			</view>
			<view class="tui-cancle" style="color: #FF3333;" @tap="search">搜索</view>
		</view>
		<view class="tui-search-history" v-show="!key">
			<view class="tui-history-header" v-show="history.length>0">
				<view class="tui-search-title">搜索历史</view>
				<tui-icon name="delete" :size='14' color='#333' @tap="openActionSheet" class="tui-icon-delete"></tui-icon>
			</view>
			<view class="tui-history-content" v-show="history.length>0">
				<block v-for="(item,index) in history" :key="index">
					<tui-tag margin="0 24rpx 24rpx 0" type="gray" shape="circle" @click="hissearch" :index="index">{{item}}</tui-tag>
				</block>
			</view>
			<view class="tui-history-header">
				<view class="tui-search-title">热门搜索</view>
			</view>
			<view class="tui-history-content">
				<block v-for="(item,index) in hot" :key="index">
					<tui-tag margin="0 24rpx 24rpx 0" type="gray" shape="circle" @click="hotsearch"  :index="index">{{item}}</tui-tag>
				</block>
			</view>
		</view>
		<view v-show="key">
			<view class="tui-header">
				<view class="tui-header-left tui-noboredr">搜索 “{{key}}”</view>
			</view>
			<view class="tui-result-box">
				<block v-for="(item,index) in searchList" :key="index">
					<view class="tui-result-item" hover-class="tui-opcity" :hover-stay-time="150" @click="resSearch" :data-val="item.key">
						<rich-text :nodes="item.showKey"></rich-text>
					</view>
				</block>
			</view>
		</view>
		<tui-actionsheet :show="showActionSheet" :tips="tips" @click="itemClick" @cancel="closeActionSheet"></tui-actionsheet>
	</view>

</template>

<script>
	const util = require("@/utils/util.js")
	export default {
		onLoad(opt) {
			this.history = this.tui.getHistory();
			this.getHot();
		},
		data() {
			return {
				history: [],
				hot: [],
				key: "",
				showActionSheet: false,
				tips: "确认清空搜索历史吗？",
				searchList:[]
			}
		},
		methods: {
			back: function() {
				uni.navigateBack();
			},
			cleanKey: function() {
				this.key = ''
			},
			closeActionSheet: function() {
				this.showActionSheet = false
			},
			openActionSheet: function() {
				this.showActionSheet = true
			},
			itemClick: function(e) {
				let index = e.index;
				if (index == 0) {
					this.showActionSheet = false;
					this.history = this.tui.clearHistory();
				}
			},
			inputKey: function(e) {
				this.key = util.trim(e.detail.value);
				if (!this.key) {
					this.searchList = [];
					return;
				}
				//根据关键词查找
				let arr = []
				let it =this;
				this.tui.request('/shop/index/searchkey','GET',{key:this.key}).then(function(res){
					if(res.code == 1){
						let _dat = res.data;
						_dat.forEach((item) => {
							arr.push({
								key: item,
								showKey: util.replaceAll(item, it.key, `<label style="color:#E41F19">${it.key}</label>`)
							})
						});
						it.searchList = arr
					}
				});
				//实际开发中，根据搜索返回结果集，此处只是做展示提示搜索哪些文字
			},
			search:function(e){
				this.tui.saveHistory(this.key);
				this.saveHot(this.key);
				this.history = this.tui.getHistory();
				uni.navigateTo({
					url:'../productList/productList?k='+this.key,
				})
			},hissearch:function(e){
				let key = this.history[e.index];
				uni.navigateTo({
					url:'../productList/productList?k='+key,
				})
			},hotsearch:function(e){
				let key = this.hot[e.index];
				uni.navigateTo({
					url:'../productList/productList?k='+key,
				})
			},
			resSearch:function(e){
				this.key = e.currentTarget.dataset.val;
				
				uni.navigateTo({
					url:'../productList/productList?k='+this.key,
				})
			},
			getHot:function(){
				let it =this;
				this.tui.request('/shop/index/hotkey').then(function(res){
					if(res.code ==1){
						it.hot = res.data;
					}
				})
			},saveHot:function(key){
				this.tui.request('/shop/index/savehot','POST',{key:key});
			},
			
		}
	}
</script>

<style>
	page {
		color: #333;
		background: #fff;
	}

	.container {
		padding: 0 30rpx 30rpx 30rpx;
		box-sizing: border-box;
	}

	.tui-searchbox {
		padding: 30rpx 0;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.tui-search-input {
		width: 100%;
		height: 66rpx;
		border-radius: 35rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		background: #f2f2f2;
		display: flex;
		align-items: center;
		flex-wrap: nowrap;
	}

	.tui-input {
		flex: 1;
		color: #333;
		padding: 0 16rpx;
		font-size: 28rpx;
	}

	.tui-input-plholder {
		font-size: 28rpx;
		color: #b2b2b2;
	}

	.tui-cancle {
		color: #888;
		font-size: 28rpx;
		padding-left: 30rpx;
		flex-shrink: 0;
	}

	.tui-history-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx 0;
	}
	.tui-history-content{
		display: flex;
		align-items: center;
		flex-wrap: wrap;
	}

	.tui-icon-delete {
		padding: 10rpx;
	}

	.tui-search-title {
		font-size: 28rpx;
		font-weight: bold;
	}

	.tui-hot-header {
		padding: 30rpx 0;
	}
	
	.tui-header {
		padding: 26rpx 0;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	
	.tui-header-left {
		font-size: 30rpx;
		color: #222;
		border-left: 4rpx solid #EB0909;
		padding-left: 10rpx;
		word-break: break-all;
	}
	
	.tui-noboredr {
		border-left: 0 !important;
	}

	.tui-result-box {
		font-size: 28rpx;
	}
	
	.tui-result-item {
		line-height: 28rpx;
		padding: 28rpx 0;
	}
</style>